package rest;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;

import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;

/**
 * cxf会显示最原始的返回信息，由使用者自己处理
 */
public class CxfRestClient {
    public static void main(String[] args) throws IOException {
        //创建webClient
        WebClient webClient = WebClient.create("http://localhost:7778/query/1334");
        //获取响应
//        Response response = webClient.get();
        Response response = webClient.post(null);
        //获取响应内容
        Object entity = response.getEntity();
        InputStream inputStream = (InputStream) entity;
        String content = IOUtils.readStringFromStream(inputStream); //源代码就是entity.toString(); 汗死！！
        System.out.println(content);
    }
    
//    public void get() {
//        //get 请求
//        String url = String.Format("http://localhost:28450/api/values?str1=a&str2=b");
//        WebClient wc = new WebClient();
//        Encoding enc = Encoding.GetEncoding("UTF-8");
//        Byte[] pageData = wc.DownloadData(url);
//        String re = enc.GetString(pageData);
//
//    }
//    public void post(){
//
////post 请求
//        String postData = "value=a";
//        byte[] bytes = Encoding.UTF8.GetBytes(postData);
//        WebClient client = new WebClient();
//        client.getHeaders().add("Content-Type", "application/x-www-form-urlencoded");
//        client.Headers.Add("ContentLength", postData.Length.ToString());
//        Encoding enc = Encoding.GetEncoding("UTF-8");
//        byte[] responseData = client.UploadData("http://localhost:28450/api/values", "POST", bytes);
//        String re = enc.GetString(responseData);
//    }
}
